﻿@{
    ViewBag.Title = string.Format("{0}-用户独立权限编辑", Request["UserName"]);
    Layout = "~/Views/Shared/_TreeLayout.cshtml";
}
@inherits CMSWebViewPage<object>
<div id="toolbar_buttoninfo" class="datagrid-toolbar">
    @CurrentButtonTool.Create()
</div>
<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center'">
        <ul id="tree_powerinfo" data-options="url:'/GroupInfo/CheckedUserDependentPowerInfoTreesJson/1?userId=@TransferID',animate:true,lines:true,checkbox:true,cascadeCheck:true"></ul>
    </div>
</div>
@section Bottom{
    <script type="text/javascript">
        var objBizTree = new BizTree("#tree_powerinfo");
        function Save() {
            var powers = GetGroupPowers();
            //alert(JSON.stringify(powers));
            //return;
            var objAjaxEntity = new AjaxEntity();
            objAjaxEntity.url = "/GroupInfo/SaveUserDependentPower?userId=@TransferID";
            objAjaxEntity.contentType = "application/json";
            objAjaxEntity.data = JSON.stringify(powers);
            objAjaxEntity.executing = function () {
                ShowProcess();
                return true;
            };
            objAjaxEntity.executeSuccess = function (InvokeResult) {
                if (InvokeResult.ResultCode == "Success") {
                    ShowMessage(InvokeResult.ResultMessage);
                    Back();
                } else {
                    ShowMessage(InvokeResult.ResultMessage);
                }
                CloseProcess();
            };
            objAjaxEntity.execute();
        }
        function Back() {
            window.location.href = "/UserInfo";
        }

        function GetGroupPowers() {
            var powerIds = [];
            var powers = [];
            var nodes = objBizTree.source.tree('getChecked');
            for (var i = 0; i < nodes.length; i++) {
                SetParent(nodes[i], powerIds, powers);
            }
            return powers;
        }
        function SetParent(node, powerIds, powers) {
            if (!node || powerIds.indexOf(node.id) >= 0) {
                return;
            }
            powerIds.push(node.id);
            var obj = new Object();
            obj.Checked = node.checked ? 1 : 0;
            obj.PowerId = node.id;
            obj.UserId = '@TransferID';
            powers.push(obj);
            var parent = objBizTree.source.tree('getParent', node.target);
            SetParent(parent, powerIds, powers);
        }
    </script>
}
